package oj;

/**
 * @author 郝文雪
 * @date 2022/7/16 16:05
 * @Description 除自身以外数组的乘积
 */
public class LC238 {
    //(nums[0]×nums[1]×...×nums[i−1])×(nums[i+1]×nums[i+2]×...×nums[n−1])
   //s1[i]表示范围[1，i]的前缀乘值，s2[i]表示[i,n]的后缀乘值
    public int[] productExceptSelf(int[] nums) {
        int n=nums.length;
        int[] s1=new int[n+10];
        int[] s2=new int[n+10];
        s1[0]=s2[n+1]=1;
        for(int i=1;i<=n;i++){
            s1[i]=nums[i-1]*s1[i-1];
        }
        for(int i=n;i>=1;i--){
            s2[i]=nums[i-1]*s2[i+1];
        }
        int[] ans=new int[n];
        for(int i=1;i<=n;i++){
            ans[i-1]=s1[i-1]*s2[i+1];
        }
        return ans;
    }
}
